/**
 * 用户groupInfo.js
 * 版本号： v1.0.0
 */
$(function(){
    var projectId = $('#prId_').val();
    var lastChecked = null;
    $("#groupInfo_dataList").datagrid({
        fitColumns: true,
        fit:true,
        method: "POST",
        rownumbers: true,
        singleSelect:true,
        pagination: true,
        border: false,
        striped:true,
        pageSize:10,
        pageList:[10,30,50,100],
        url: CONTEXT_PATH + '/groupInfo/groupInfoList.do?projectId='+projectId,
        idField: 'groupId',
        frozenColumns:[[
            {field:'groupId',checkbox:true}
        ]],
        columns: [
            [
                {field: 'projectId', title: '项目id', width: 180, fixed: true,hidden:true},
                {field: 'projectName', title: '所属项目', width: 180, fixed: true},
                {field: 'groupName', title: '班组名称', width: 100, fixed: true},
                {field: 'groupLeader', title: '班组负责人', width: 80, fixed: true,
                    formatter:function(val){
                        if(val){
                            return val;
                        }
                    }
                },
                {field: 'groupPersonCount', title: '班组人数', width: 60}
            ]
        ],
        onSelect:function(rowIndex, rowData){
        },
        onCheck:function(rowIndex, rowData){
            var row = $("#groupInfo_dataList").datagrid("getChecked");
            if(row && rowIndex == lastChecked){
                $('#groupInfo_dataList').datagrid("unselectAll");//取消选中当前所有行
                lastChecked = null;
            }else{
                lastChecked = rowIndex;
            }
        },
        onLoadSuccess:function(data){
            if(data.rows.length > 0){
                lastChecked = null;
                $("#groupInfo_dataList").datagrid("selectRow", 0);
            }
        }
    });

    //定义可操作按钮
    $("#groupInfo_menu").dialog({
        title: '',
        width: 'auto',
        height:35,
        closed: false,
        cache: false,
        inline: true,
        modal: false,
        border: false,
        toolbar: createToolbar()
})

});

/**
 * 创建toolbar按钮组
 * @returns {Array}
 */
function createToolbar() {
    var toolbar = [];
    var falg = false;
    for (var j = 0; j < btnOptions.length; j++) {
        if (false != btnOptions[j].value) {
            falg = true;
            break;
        }
    }
    if (!falg) {
        //不存在按钮
        toolbar.push({});
    } else {
        //存在按钮
        for (var i = 0; i < btnOptions.length; i++) {
            if (false != btnOptions[i].value) {
                //查询按钮
                if ("searchBtn" == btnOptions[i].key) {
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            groupInfo_findList();
                        }
                    });
                }
                //查询班组成员
                if ("viewBtn" == btnOptions[i].key) {
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            getGroupMember();
                        }
                    });
                }

            }
        }
    }
    return toolbar;
}

/**
 * 查看班组成员
 * @param index
 */
function getGroupMember(){
    var checked = $("#groupInfo_dataList").datagrid("getChecked");
    if (!checked || checked.length == 0) {x
        $.messager.show({ title: '操作提示', msg: "请选择一条数据后再进行修改操作", timeout: 3000, showType: "slide" });
        return;
    };
    var row = $('#groupInfo_dataList').datagrid('getSelected');
    var groupId = row.groupId;
    var groupName = row.groupName;
    var projectName = row.projectName;
    var groupPersonCount = row.groupPersonCount;
    var projectId = row.projectId;
    window.location.href = encodeURI(bzxq.url +
        "groupId="+groupId+'&groupName='+groupName+"&projectName="+projectName+'&groupPersonCount='+groupPersonCount+
        '&projectId='+projectId);
}


//查询班组成员
function groupInfo_findList(){
    //刷新dg页码
    refreshPageNumber("groupInfo_dataList");
    $("#groupInfo_dataList").datagrid("reload",{groupName:$("#groupName_").textbox("getValue")});
    //清除选中项
    $("#groupInfo_dataList").datagrid("clearSelections");
}